package com.example.controller;

import Resume.Dto.Resume;
import Resume.Dto.ResumeJobMap;
import Resume.Dto.ResumeJobResponse;
import Resume.Dto.ResumeUser;
import com.example.mapper.FileClient;
import com.example.mapper.JobClient;
import com.example.mapper.ResumeMapper;
import com.example.mapper.UserClient;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pojo.Result;
import pojo.Kmoji;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/resume")
@RequiredArgsConstructor
public class ResumeController {
    final ResumeMapper resumeMapper;
    @Autowired
    JobClient jobClient;
    @Autowired
    UserClient userClient;
    final FileClient fileClient;

    @GetMapping("/auditResume/{resumeId}/{status}")
    public void auditResume(@PathVariable("resumeId") int resumeId,@PathVariable("status") int status){
        resumeMapper.auditResume(resumeId,status);
    }

    @GetMapping("/getResumeJobResponse/all")
    public List<ResumeJobResponse> getResumeJobMapAll() {
        List<ResumeJobMap> list=resumeMapper.getResumeJobMapAll();
        List<ResumeJobResponse> responsesList=new ArrayList<>();
        for(ResumeJobMap resumeJobMap:list) {
            ResumeJobResponse response = new ResumeJobResponse();
            response.setId(resumeJobMap.getId());
            response.setUserAccount(userClient.getAccountById(resumeJobMap.getUserId()));
            response.setJobDto(jobClient.findById(resumeJobMap.getJobId()));
            response.setResume(resumeMapper.getResumeById(resumeJobMap.getResumeId()));
            response.setDeliverTime(resumeJobMap.getDeliverTime());
            responsesList.add(response);
        }
        return responsesList;
    }

    @GetMapping("/getResumeJobResponse/{companyId}/{status}")
    public List<ResumeJobResponse>
    getResumeJobMapByCompanyId(
            @PathVariable("companyId") int companyId,
            @PathVariable("status") int status) {
        List<ResumeJobMap> list;
        if(status==3) {
           list = resumeMapper.getResumeJobMapByCompanyIdAll(companyId);
        }else{
           list=resumeMapper.getResumeJobMapByCompanyId(companyId,status);
        }
        List<ResumeJobResponse> responsesList=new ArrayList<>();
        for(ResumeJobMap resumeJobMap:list) {
            ResumeJobResponse response = new ResumeJobResponse();
            response.setId(resumeJobMap.getId());
            response.setUserAccount(userClient.getAccountById(resumeJobMap.getUserId()));
            response.setJobDto(jobClient.findById(resumeJobMap.getJobId()));
            response.setResume(resumeMapper.getResumeById(resumeJobMap.getResumeId()));
            response.setDeliverTime(resumeJobMap.getDeliverTime());
            responsesList.add(response);
        }
        return responsesList;
    }


    @GetMapping("/getResumeJobResponseYlx/{userId}/{status}")
    public List<ResumeJobResponse>
    getResumeJobMapByUserId(
            @PathVariable("userId") int userId,
            @PathVariable("status") int status) {
        List<ResumeJobMap> list;
        list=resumeMapper.getResumeJobMapByUserId(userId,status);
        List<ResumeJobResponse> responsesList=new ArrayList<>();
        for(ResumeJobMap resumeJobMap:list) {
            ResumeJobResponse response = new ResumeJobResponse();
            response.setId(resumeJobMap.getId());
            response.setUserAccount(userClient.getAccountById(resumeJobMap.getUserId()));
            response.setJobDto(jobClient.findById(resumeJobMap.getJobId()));
            response.setResume(resumeMapper.getResumeById(resumeJobMap.getResumeId()));
            response.setDeliverTime(resumeJobMap.getDeliverTime());
            responsesList.add(response);
        }
        return responsesList;
    }
    //根据UserID查所有的简历
    @GetMapping("/user/{user_id}")
    public Result<List<Resume>> findResumeByUserId(@PathVariable("user_id") int user_id) {
        try {
            List<Integer> ints = resumeMapper.getResumeIdByUserId(user_id);
//            System.out.println(ints);
            List<Resume> resumes = new ArrayList<>();
            for (Integer resumeId : ints) {
                Resume resume = resumeMapper.getResumeById(resumeId);
                if (resume!= null) {
                    resumes.add(resume);
                }
            }
            if (!resumes.isEmpty()) {
                return Result.success(resumes);
            } else {
                return Result.fail("未找到对应简历");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    //根据UserID和Status查所有的简历集合
    @GetMapping("/user_status/{user_id}/{status}")
    public Result<List<Resume>> findResumeByUserIdAndStatus(@PathVariable("user_id") int user_id, @PathVariable("status") int status) {
        try {
            List<Integer> ints = resumeMapper.getResumeIdByUserIdAndStatus(user_id, status);
            List<Resume> resumes = new ArrayList<>();
            for (Integer resumeId : ints) {
                Resume resume = resumeMapper.getResumeById(resumeId);
                if (resume!= null) {
                    resumes.add(resume);
                }
            }
            if (!resumes.isEmpty()) {
                return Result.success(resumes);
            } else {
                return Result.fail("未找到对应简历");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }


    @GetMapping("{resume_id}")
    public Result<Resume> findResumeByResumeId (@PathVariable int resume_id) {
        try {
            Resume resume = resumeMapper.getResumeById(resume_id);
            if (resume!= null) {
                return Result.success(resume);
            } else {
                return Result.fail("未找到对应简历");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    @GetMapping("/by_name")
    public Result<List<Resume>> findResumeByUsername (@RequestParam("user_name") String user_name) {
        try {
            List<Resume> resumes = resumeMapper.getResumesByName(user_name);
            if (resumes!= null) {
                return Result.success(resumes);
            } else {
                return Result.fail("未找到对应简历");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    @GetMapping()
    public Result<List<Resume>> getAllResumes() {
        try {
            List<Resume> resumes = resumeMapper.getResumes();
            return Result.success(resumes);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }


    @PostMapping("/upload_resume")
    public Result<Integer> uploadResume(@RequestBody ResumeUser resumeUser) {
        System.out.println(resumeUser);
        Resume resume = resumeUser.getResume();
        try {
            resumeMapper.insertResume(resume);
            Integer resumeId = resume.getId();

            System.out.println(resumeId);
            System.out.println(resumeUser.getUserId());
            resumeMapper.addResumeUser(resumeId,resumeUser.getUserId());


            return Result.success(resume.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    @PostMapping("/update")
    public Result<Integer> updateResume(@RequestBody Resume resume) {
        try {
            int rowsAffected = resumeMapper.updateResume(resume);
            if (rowsAffected > 0) {
                return Result.success(rowsAffected);
            } else {
                return Result.fail("更新失败，未找到对应简历");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    @PostMapping("/delete/{resume_id}")
    public Result<?> deleteResume(@PathVariable int resume_id) {
        try {
            resumeMapper.deleteResume(resume_id);
                return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    @PostMapping("/resume_url/{resume_id}")
    public Result<?> updateResumeUrl(@PathVariable int resume_id, @RequestPart("file") MultipartFile multipartFile) {
        try {
            String resume_url = fileClient.uploadVideos(multipartFile);
            resumeMapper.updateResumeUrl(resume_id, resume_url);
            return Result.success();

        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(500, "服务器内部错误");
        }
    }

    //将头像，pdf上传到七牛云并返回地址
    @PostMapping("/upload")
    public Result<String> uploadFile(@RequestPart("file") MultipartFile multipartFile) {
        if (multipartFile.isEmpty()) {
            return Result.fail("请选择要上传的文件");
        }
        try {
            String file_url = fileClient.upload(multipartFile);
            return Result.success(file_url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("文件上传失败");
        }
    }

    @PostMapping("/submit_job") // 投递简历
    public void submitJob(@RequestBody Kmoji kmoji){
        System.out.println(kmoji);
        resumeMapper.submitJob(kmoji);
    }
}
